Friend Class SharedObjects
    Friend Shared Function GetFileNameWPath(ByVal FilePath As String, _
                                            ByVal FileName As String) As String
        Try
            'Trim up the filename stuff
            FilePath = FilePath.Trim
            FileName = FileName.Trim

            'Condition Path
            If Not FilePath.EndsWith("\") Then
                FilePath = FilePath & "\"
            End If

            'Build filename with path
            Return FilePath & FileName
        Catch ex As Exception
            Throw New Exception("FilePath: """ & FilePath & """ FileName: """ & FileName & """", ex)
        End Try

        Return ""
    End Function

    Friend Shared Function BreakFileNameWithPathForFilePath(ByVal FileNameWithPath As String) As String
        FileNameWithPath = FileNameWithPath.Trim

        Try
            Return System.IO.Path.GetDirectoryName(FileNameWithPath).Trim & "\"
        Catch ex As Exception
            Throw New Exception("FileName: " & FileNameWithPath, ex)
        End Try

        Return Nothing
    End Function

    Friend Shared Function FolderExistsCheck(ByVal FullDirectoryPath As String) As Boolean
        Dim _bFolderExists As Boolean = False

        Try
            Dim _oAttributes As System.IO.FileAttributes

            _oAttributes = System.IO.File.GetAttributes(FullDirectoryPath)

            _bFolderExists = True
        Catch FileNotFFoundex As System.IO.FileNotFoundException
            _bFolderExists = False
        Catch DirNotFoundex As System.IO.DirectoryNotFoundException
            _bFolderExists = False
        Catch ex As Exception
            _bFolderExists = False

            Throw New Exception("", ex)
        End Try

        Return _bFolderExists
    End Function

    Friend Shared Function FileExistsCheck(ByVal FilePath As String, ByVal FileName As String) As Boolean
        Dim _sFileNameWithPath As String = GetFileNameWPath(FilePath, FileName)

        Try
            Return FileExistsCheck(_sFileNameWithPath)
        Catch ex As Exception
            Throw New Exception("FileName: " & _sFileNameWithPath, ex)
        End Try
    End Function

    Friend Shared Function FileExistsCheck(ByVal FileNameWithPath As String) As Boolean
        Try
            Dim _FileInfo As New System.IO.FileInfo(FileNameWithPath)

            Return _FileInfo.Exists
        Catch ex As Exception
            Throw New Exception("FileName: " & FileNameWithPath, ex)
        End Try

        Return False
    End Function

    Friend Shared Sub CopyFile(ByVal SourceFilePath As String, _
                               ByVal SourceFileName As String, _
                               ByVal DestinationFilePath As String, _
                               ByVal DestinationFileName As String, _
                               ByVal OverwriteExistingFileFlag As Boolean)
        Dim _sSourceFileNameWithPath As String = GetFileNameWPath(SourceFilePath, SourceFileName)
        Dim _sDestinationFileNameWithPath As String = GetFileNameWPath(DestinationFilePath, DestinationFileName)

        Try
            System.IO.File.Copy(_sSourceFileNameWithPath, _sDestinationFileNameWithPath, OverwriteExistingFileFlag)
        Catch ex As Exception
            Throw New Exception("SourceFileNameWithPath: """ & _sSourceFileNameWithPath & """ DestinationFileNameWithPath: """ & _sDestinationFileNameWithPath & """", ex)
        End Try
    End Sub

    Friend Shared Sub MoveFile(ByVal SourceFilePath As String, _
                               ByVal SourceFileName As String, _
                               ByVal DestinationFilePath As String, _
                               ByVal DestinationFileName As String, _
                               ByVal OverwriteExistingFileFlag As Boolean)
        Dim _sSourceFileNameWithPath As String = GetFileNameWPath(CType(SourceFilePath, String), CType(SourceFileName, String))
        Dim _sDestinationFileNameWithPath As String = GetFileNameWPath(CType(DestinationFilePath, String), CType(DestinationFileName, String))

        Try
            If OverwriteExistingFileFlag = True Then
                DeleteFile(_sDestinationFileNameWithPath)
            End If

            System.IO.File.Move(_sSourceFileNameWithPath, _sDestinationFileNameWithPath)
        Catch ex As Exception
            Throw New Exception("SourceFileNameWithPath: """ & _sSourceFileNameWithPath & """ DestinationFileNameWithPath: """ & _sDestinationFileNameWithPath & """", ex)
        End Try
    End Sub

    Friend Shared Sub DeleteFile(ByVal FilePath As String, ByVal FileName As String)
        Dim _sFileNameWithPath As String = GetFileNameWPath(CType(FilePath, String), CType(FileName, String))

        Try
            DeleteFile(_sFileNameWithPath)
        Catch ex As Exception
            Throw New Exception("FileName: " & _sFileNameWithPath, ex)
        End Try
    End Sub

    Friend Shared Sub DeleteFile(ByVal FileNameWithPath As String)
        Try
            If FileExistsCheck(FileNameWithPath) = True Then
                System.IO.File.Delete(FileNameWithPath)
            End If
        Catch ex As Exception
            Throw New Exception("FileName: " & FileNameWithPath, ex)
        End Try
    End Sub

    Friend Shared Sub DeleteFiles(ByVal FilePath As String, ByVal FilePattern As String)
        Try
            Dim _sFilePath As String
            Dim _sFilePattern As String
            Dim _sFileNameFound As String

            If Right(FilePath.Trim, 1) = "\" Then
                _sFilePath = FilePath.Trim
            Else
                _sFilePath = FilePath.Trim & "\"
            End If

            _sFilePattern = FilePattern.Trim

            For Each _sFileNameFound In System.IO.Directory.GetFiles(_sFilePath, _sFilePattern)
                System.IO.File.Delete(_sFileNameFound)
            Next
        Catch ex As Exception
            Throw New Exception("FilePath: " & FilePath.Trim & " Pattern: " & FilePattern.Trim, ex)
        End Try
    End Sub

    Friend Shared Sub GetFileSpecs(ByVal FileName As String, _
                                   ByVal FilePath As String, _
                                   ByRef FileSize As Integer, _
                                   ByRef FileDt As Date, _
                                   ByRef FileExistFlag As Boolean)
        Dim _sFileNameWithPath As String = GetFileNameWPath(FilePath, FileName)

        Try
            GetFileSpecs(_sFileNameWithPath, FileSize, FileDt, FileExistFlag)
        Catch ex As Exception
            Throw New Exception("FileName: " & _sFileNameWithPath, ex)
        End Try
    End Sub

    Friend Shared Sub GetFileSpecs(ByVal FileNameWithPath As String, _
                                   ByRef FileSize As Integer, _
                                   ByRef FileDt As Date, _
                                   ByRef FileExistFlag As Boolean)
        Try
            Dim _FileInfo As New System.IO.FileInfo(FileNameWithPath)

            If _FileInfo.Exists Then
                FileExistFlag = True
                FileSize = CInt(_FileInfo.Length)
                FileDt = _FileInfo.LastWriteTime
            Else
                FileExistFlag = False
            End If
        Catch ex As Exception
            Throw New Exception("FileName: " & FileNameWithPath, ex)
        End Try
    End Sub

    Friend Shared Function GetFileImage(ByVal FilePath As String, ByVal FileName As String) As Byte()
        Dim _sFileNameWithPath As String = GetFileNameWPath(FilePath, FileName)

        Try
            Return GetFileImage(_sFileNameWithPath)
        Catch ex As Exception
            Throw New Exception("FileName: " & _sFileNameWithPath, ex)
        End Try
    End Function

    Friend Shared Function GetFileImage(ByVal FileNameWithPath As String) As Byte()
        Try
            If FileExistsCheck(FileNameWithPath) = False Then
                Throw New Exception("File does not exist to get the file image.")
            End If

            Dim _oFIO As New IO.FileStream(FileNameWithPath, IO.FileMode.Open)
            Dim _oInbuff(CType(_oFIO.Length - 1, Integer)) As Byte

            _oFIO.Read(_oInbuff, 0, CType(_oFIO.Length, Integer))
            _oFIO.Close()

            Return _oInbuff
        Catch ex As Exception
            Throw New Exception("FileName: " & FileNameWithPath, ex)
        End Try
    End Function

    Friend Shared Sub SaveFileImage(ByVal FilePath As String, ByVal FileName As String, ByRef FileBytes As Byte())
        Dim _sFileNameWithPath As String = GetFileNameWPath(FilePath, FileName)

        Try
            SaveFileImage(_sFileNameWithPath, FileBytes)
        Catch ex As Exception
            Throw New Exception("FileName: " & _sFileNameWithPath, ex)
        End Try
    End Sub

    Friend Shared Sub SaveFileImage(ByVal FileNameWithPath As String, ByRef FileBytes As Byte())
        Try
            Dim fio As New IO.FileStream(FileNameWithPath, IO.FileMode.Create)

            fio.Write(FileBytes, 0, FileBytes.Length)

            fio.Close()
        Catch ex As Exception
            Throw New Exception("FileName: " & FileNameWithPath, ex)
        End Try
    End Sub
End Class
